package com.basic.library.utils.audio;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;

/**
 * @author yj
 * @description
 */
public class MediaManager {
    private static MediaManager mediaManager = new MediaManager();

    public static MediaManager getInstance() {
        return mediaManager;
    }

    private MediaPlayer mPlayer;
    private AudioManager am;
    private boolean isPause;

    public void playSound(Context context, String filePathString,
                          OnListener onListener) {
        try {
            MAudioManager.getInstance().initAudioManager(context);
            if (mPlayer == null) {
                mPlayer = new MediaPlayer();
            } else {
                mPlayer.reset();//就恢复初始
            }
            //保险起见，设置报错监听
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    // TODO Auto-generated method stub
                    if (onListener != null)
                        onListener.onError("失败" + what + "--" + extra);
                    mPlayer.reset();
                    MAudioManager.getInstance().muteAudioFocus(false);
                    return false;
                }
            });
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    if (onListener != null)
                        onListener.onComplete();
                    MAudioManager.getInstance().muteAudioFocus(false);
                }
            });
            mPlayer.setDataSource(filePathString);
            mPlayer.prepareAsync();
            mPlayer.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    mPlayer.start();//System.out.println("时长："+mPlayer.getDuration());
                    if (onListener != null)
                        onListener.onStart();
                }
            });
//			mPlayer.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (onListener != null)
                onListener.onError(e.getMessage());
        }
    }

    //停止函数
    public void pause() {
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.pause();
            isPause = true;
        }
    }

    //继续
    public void resume() {
        if (mPlayer != null && isPause) {
            mPlayer.start();
            isPause = false;
        }
    }


    public void release() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    public interface OnListener {
        void onStart();

        void onComplete();

        void onError(String e);
    }
}